package com.dayatang.android.Activity;

import java.util.concurrent.ExecutionException;

import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import com.dayatang.android.Activity.R.color;
import com.dayatang.android.Chart.AbstractChart;

public class ProjectOutputallByYearActivity extends AbstractChart{

	private String[] projectName = null;
	private String[] projectOutput = null;
	
	private int fromYear,toYear;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		InitEditText("byYear");
	}
	
	protected void InitData(){
		
		String url = getServerAddress() + "/remote/project-outputall-byyear.action?fromYear="+fromYear+"&toYear="+toYear;
		
		try {
			String result = CheckServerData(url);
			
			if(result != null){
				
				JSONArray array = new JSONArray(result);

				projectName = new String[array.length()];
				projectOutput = new String[array.length()];

				for (int i = 0, len = array.length(); i < len; i++) {

					JSONObject jsonObject = array.getJSONObject(i);
					projectName[i] = jsonObject.getString("projectX");
					projectOutput[i] = jsonObject.getString("projectOutput");

				}
				FillContentView(execute());
				
			}
			
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		
	}
	
	public View execute() {
		
		XYMultipleSeriesRenderer renderer = BuildBarRenderer(Color.YELLOW);
		
		setChartSettings(renderer,getString(R.string.reports_tab_two)+getString(R.string.reports_statistics),getString(R.string.reports_x_axis_byyear),getString(R.string.reports_y_axis_output),0.5,8.5,0,getArrayMax(projectOutput)*1.1);
		
	    for (int i = 1, len = projectOutput.length; i <= len; i++ ) {
	    	renderer.addXTextLabel(i, projectName[i-1]);
	    }
	    
		return ChartFactory.getBarChartView(this, BuildBarDataset(projectName,projectOutput), renderer, Type.DEFAULT);
	}

//	protected Dialog onCreateDialog(int id) {
//		
//		Dialog dialog = null;
//		
//		switch(id){
//		
//			case StartDATE_DIALOG:
//				c = Calendar.getInstance();
//				dialog = new DatePickerDialog(this, 
//						new DatePickerDialog.OnDateSetListener() {
//							public void onDateSet(DatePicker view, int year, int monthOfYear,
//									int dayOfMonth) {
//									
//									//fromEditText.setText(year);
//							}
//						}, 
//					c.get(Calendar.YEAR), 
//					c.get(Calendar.MONTH), 
//					c.get(Calendar.DAY_OF_MONTH)
//				);
//				break;
//			
//			case EndDATE_DIALOG:
//				c = Calendar.getInstance();
//				dialog = new DatePickerDialog(this,
//						new DatePickerDialog.OnDateSetListener() {
//							public void onDateSet(DatePicker view, int year, int monthOfYear,
//									int dayOfMonth) {
//									//toEditText.setText(year);
//							}
//						}, 
//					c.get(Calendar.YEAR), 
//					c.get(Calendar.MONTH), 
//					c.get(Calendar.DAY_OF_MONTH)
//				);
//				break;
//				
//		}
//		
//		return dialog;
//
//	}
	

}
